package com.hangwei.aicabinet.frament;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangwei.aicabinet.R;
import com.hangwei.aicabinet.adapter.SubOrderAdapter;
import com.hangwei.aicabinet.model.bean.OrderBean;
import com.hangwei.aicabinet.model.entry.LockOpenEvent;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.view.BaseView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * author: 张龙臣
 * date: 2018-08-27.
 */

public class SubOrderFragment extends Fragment implements BaseView {

    private SubOrderAdapter mAdapter;
    private FrameLayout mFlEmpty;
    private OnFragmentAddedLisenter mAddedLisenter;
    private TextView tvNum;
    private Dialog dialog;
    private TextView tvOrderNum;
    private TextView tvStatus;

    public void setAddedLisenter(OnFragmentAddedLisenter addedLisenter) {
        mAddedLisenter = addedLisenter;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sub_order, null);
        ImageView ivOpenAll = view.findViewById(R.id.ivOpenAll);
        tvNum = view.findViewById(R.id.tvNum);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        tvOrderNum = view.findViewById(R.id.tvOrderNum);
        tvStatus = view.findViewById(R.id.tvStatus);
//        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
//        refreshLayout.setOnRefreshListener(this);
        mFlEmpty = view.findViewById(R.id.flEmpty);
        mAdapter = new SubOrderAdapter();
        String type = SPUtil.getString(SPKey.CABINET_NUM);

        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i("info", "landscape-----横屏"); // 横屏
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.i("info", "portrait------竖屏"); // 竖屏
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        }
//        if ("YD".equals(type.substring(3, 5)))
//            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
//        else recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recyclerView.setAdapter(mAdapter);
//        ivOpenAll.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ToastUtil.showCenterToast("柜门已关闭");
//                NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
//                    @Override
//                    protected Observable<ResponseInfo<String>> getObservable() {
//                        return NetManager.getResponseInfoAPI().updateLockStatus("LOCK_001", 0);
//                    }
//
//                    @Override
//                    protected void onNext(String data) {
//                        LogUtil.d("关锁成功");
//                    }
//
//                    @Override
//                    protected void onError() {
//
//                    }
//                });
//
////                LoopManager.loop();
//                SerialPortProxy.setAntenna(InstructionsUtil.buildSetAntennaOrder(1));
//                if (getActivity() != null) {
////                    dialog = LoadingDialog.showDialog(getActivity(), "工具检测中");
////                    dialog.show();
//                }
//                //三秒后拿到标签
////                App.getmHandler().postDelayed(new Runnable() {
////                    @Override
////                    public void run() {
////                        upLoadTab();
////                    }
////                }, 5000);
//            }
//        });
        return view;
    }
//
//    private void upLoadTab() {
//        final String rf = getRFString(SerialPortProxy.getAllTab());
//
//        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
//            @Override
//            protected Observable<ResponseInfo<String>> getObservable() {
//                return NetManager.getResponseInfoAPI().doCheckOrders(rf, MainActivity.id, "LOCK_001");
//            }
//
//            @Override
//            protected void onNext(String data) {
//                LogUtil.d("校验成功");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
//                ToastUtil.showCenterToast("工具检测完毕");
//            }
//
//            @Override
//            protected void onError() {
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
//            }
//        });
//    }

//    private String getRFString(HashMap<String, String> allTab) {
//        Set<String> keySet = allTab.keySet();
//        if (keySet.size() == 0)
//            return "";
//        StringBuilder sb = new StringBuilder();
//        for (String key : keySet) {
//            sb.append(key).append(",");
//        }
//        String s = sb.toString();
//        return s.substring(0, s.length() - 1);
//    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mAddedLisenter != null)
            mAddedLisenter.onFragmentAdded();
    }


    public void setEmptyView() {
        mFlEmpty.setVisibility(View.VISIBLE);
        tvOrderNum.setText("");
        tvStatus.setText("");
    }

    @SuppressLint("SetTextI18n")
    public void setData(List<OrderBean.EntryListBean> entryList, String orderNum) {
        mFlEmpty.setVisibility(View.GONE);
        mAdapter.setNewData(entryList);
        tvNum.setText("" + entryList.size());
        tvOrderNum.setText(orderNum);
        tvStatus.setText("未完成");
    }

    public interface OnFragmentAddedLisenter {
        void onFragmentAdded();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLockOpenEvent(LockOpenEvent event) {
        int which = SPUtil.getInt(SPKey.OPENED_DOOR);
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_open_door, null);
        ImageView imageView = inflate.findViewById(R.id.ivOpenDoor);
        switch (which) {
            case 1:
                imageView.setImageResource(R.drawable.cabinet2);
                break;
            case 2://2号门在一号门的位置
                imageView.setImageResource(R.drawable.cabinet1);
                break;
            case 3:
                imageView.setImageResource(R.drawable.cabinet3);
                break;
            case 4:
                imageView.setImageResource(R.drawable.cabinet4);
                break;
            case 5:
                imageView.setImageResource(R.drawable.cabinet5);
                break;
            case 6:
                imageView.setImageResource(R.drawable.cabinet6);
                break;
        }
        final Dialog dialog = new Dialog(getActivity(), R.style.generic_dialog);
//        dialog.setContentView(inflate);
        dialog.setCancelable(false);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(inflate, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        Log.i("---AAAAAAAAAAAA--", "a------" + which);
        dialog.show();
//        App.getmHandler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if (dialog.isShowing())
//                    dialog.dismiss();
//            }
//        }, 3000);
    }
}
